﻿using System;
using System.Windows;
using System.Windows.Controls;

using SBPweb.Forge.Controls.Silverlight.Utilities;

using SBPweb.Communication.Shared.Library.Contacts;

using SBPweb.Communication.Client.Controls.Xmpp.Themes;

namespace SBPweb.Communication.Client.Controls.Xmpp.Connections
{

    [TemplatePart(Name = "Content", Type = typeof(ConnectionsView))]
    public partial class ConnectionsView : UserControlBase
    {

        #region Private fields

        #endregion

        #region Constructor logic

        public ConnectionsView()
            : base()
        {
            InitializeComponent();
            Style = Generic.Instance["xmppConnectionsView"] as Style;

            if (Messenger != null)
            {
                Messenger.Initialized += new EventHandler(Messenger_Initialized);
                Messenger.Connections.PresenceStateChanged += new EventHandler<ContactPresenceStateBaseEventArgs>(Connections_PresenceStateChanged);
            }
            PresenceSelectionChanged += new SelectionChangedEventHandler(cbPresence_SelectionChanged);
            if (string.IsNullOrEmpty(ImageSourceBindingPathFormat))
            {
                ImageSourceBindingPathFormat = "/Resources/Contact_{0}_64x64.png";
            }
        }

        #endregion

        #region Property accessors

        public string ImageSourceBindingPathFormat
        {
            get
            {
                if (imageSourceConverter == null)
                {
                    imageSourceConverter = Resources["imageSourceConverter"] as ImageSourceConverter;
                }
                return imageSourceConverter.ImageSourceBindingPathFormat;
            }
            set
            {
                imageSourceConverter.ImageSourceBindingPathFormat = value;
            }
        }

        #endregion

        #region Events

        public event SelectionChangedEventHandler PresenceSelectionChanged;

        public event EventHandler PresenceSelectionOpened
        {
            add
            {
                cbPresence.DropDownOpened += value;
            }
            remove
            {
                cbPresence.DropDownOpened -= value;
            }
        }

        public event EventHandler PresenceSelectionClosed
        {
            add
            {
                cbPresence.DropDownClosed += value;
            }
            remove
            {
                cbPresence.DropDownClosed -= value;
            }
        }

        #endregion
                               
        #region Methods

        #region Event handlers

        private void Messenger_Initialized(object sender, EventArgs e)
        {
            cbPresence.ItemsSource = Messenger.Connections.EnumeratePresenceStates();
        }

        private void Connections_PresenceStateChanged(object sender, ContactPresenceStateBaseEventArgs e)
        {
            this.Dispatcher.BeginInvoke(delegate
            {
                cbPresence.SelectedItem = e.State;
            });
        }

        private void cbPresence_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.RemovedItems.Count > 0 && e.AddedItems[0].Equals(e.RemovedItems[0]))
            {
                return;
            }
            Messenger.Connections.SetPresenceState(cbPresence.SelectedItem as ContactPresenceStateBase);
        }

        internal void OnPresenceSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (PresenceSelectionChanged != null)
            {
                PresenceSelectionChanged(sender, e);
            }
        }

        #endregion

        #endregion

    }

}